using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class MutasiWarehouseDTO : INotifyPropertyChanged
    {
        private String _Description;
        private WarehouseDTO _FromWarehouse;
        private ObservableCollection<MutasiWarehouseDetailDTO> _MutasiWarehouseDetails;
        private int _No;
        private String _NoMutasi;


        private DateTime? _TglMutasi;
        private WarehouseDTO _ToWarehouse;
        private String _created_by;
        private DateTime? _created_date;
        private String _modified_by;
        private DateTime? _modified_date;

        public MutasiWarehouseDTO()
        {
            TglMutasi = DateTime.Now.Date;
        }

        [DataMember]
        public String NoMutasi
        {
            get { return _NoMutasi; }
            set
            {
                _NoMutasi = value;
                RaisePropertyChanged("NoMutasi");
            }
        }

        [DataMember]
        public DateTime? TglMutasi
        {
            get { return _TglMutasi; }
            set
            {
                _TglMutasi = value;
                RaisePropertyChanged("TglMutasi");
            }
        }


        [DataMember]
        public WarehouseDTO FromWarehouse
        {
            get { return _FromWarehouse; }
            set
            {
                _FromWarehouse = value;
                RaisePropertyChanged("FromWarehouse");
            }
        }


        [DataMember]
        public WarehouseDTO ToWarehouse
        {
            get { return _ToWarehouse; }
            set
            {
                _ToWarehouse = value;
                RaisePropertyChanged("ToWarehouse");
            }
        }


        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public ObservableCollection<MutasiWarehouseDetailDTO> MutasiWarehouseDetails
        {
            get { return _MutasiWarehouseDetails; }
            set
            {
                if (value == MutasiWarehouseDetails) return;
                _MutasiWarehouseDetails = value;
                RaisePropertyChanged("MutasiWarehouseDetails");
                RefreshNumber();
            }
        }


        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }


        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RefreshNumber()
        {
            MutasiWarehouseDetails.CollectionChanged += MutasiWarehouseDetails_CollectionChanged;
            int number = 0;
            foreach (MutasiWarehouseDetailDTO DetailDto in MutasiWarehouseDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        private void MutasiWarehouseDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (MutasiWarehouseDetailDTO DetailDto in MutasiWarehouseDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}